from datetime import date
from datetime import time
from datetime import datetime


# strftime 格式化时间字符串, 格式很多：https://www.cainiaojc.com/python/python-datetime-strftime.html
def get_date():
    today = date.today()
    # yyyy = today.year
    # mm = today.month
    # dd = today.day
    # print("{}/{}/{}".format(yyyy, mm, dd)) # 2025/5/16
    return today.strftime("%Y-%m-%d")

print(get_date()) # 2025-05-16

def change_date(date_str):
    year, month, day = date_str.split("-")
    new_date = date(int(year), int(month), int(day))
    return new_date.strftime("%Y/%m/%d")
print(change_date('2025-05-16')) # 2025/05/16

# 时间戳转换为日期时间格式
def timestamp_to_date(timestamp):
    return date.fromtimestamp(timestamp)
print(timestamp_to_date(1628555200)) # 2021-08-10


def get_time():
    current_time = time()
    hh = current_time.hour
    mm = current_time.minute
    ss = current_time.second
    print("{}:{}:{}".format(hh, mm, ss)) # 00:00:00

    b = time(10,30,45)
    c = time(hour = 11, minute = 34, second = 56)
    print(b) # 10:30:45
    print(c) # 11:34:56
    return current_time.strftime("%H:%M:%S")

print(get_time()) # 14:50:00

# 处理时区问题  pytZ模块
import pytz
def get_timezone():
    # 获取所有时区
    for tz in pytz.all_timezones:
        print('tz = ',tz)
    # 获取当前时区
    print(pytz.timezone('Asia/Shanghai')) # Asia/Shanghai
    # 获取当前时间
    print(pytz.timezone('Asia/Shanghai').localize(datetime.now())) # 2025-05-16 14:50:00.000000+08:00
    # 获取当前时间的UTC时间
    print(pytz.timezone('Asia/Shanghai').localize(datetime.now()).astimezone(pytz.utc)) # 2025-05-16 06:50:00.000000+00:00
    # 获取当前时间的UTC时间
    print(pytz.timezone('Asia/Shanghai').localize(datetime.now()).astimezone(pytz.timezone('Asia/Shanghai'))) # 2025-05-16 14:50:00.000000+08:00
    # 获取当前时间的UTC时间
    print(pytz.timezone('Asia/Shanghai').localize(datetime.now()).astimezone(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S")) # 2025-05-16 14:50:00
    # 获取当前时间的UTC时间
    return pytz.timezone('Asia/Shanghai').localize(datetime.now()).astimezone(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S %Z") # 2025-05-16 14:50:00 CST


# 时间戳 timestamp
def get_timestamp():
    now = datetime.now()

    timestamp = datetime.timestamp(now)
    print("时间戳 =", timestamp)
    return timestamp


# sleep 函数
import time
def sleep_test():
    print("开始")
    time.sleep(2) # 暂停2秒
    print("结束")
sleep_test() # 开始 结束